package backend

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"miaoyuadmin/api/backend"
	"miaoyuadmin/api/backend/response"
	"miaoyuadmin/internal/service"
	function "miaoyuadmin/utility"
)

var Common = new(commonCtl)

type commonCtl struct {
}

func (c *commonCtl) Captcha(ctx context.Context, req *backend.CaptchaDoReq) (res *backend.CaptchaDoRes, err error) {
	//parse request parameters

	idKey, b64s, capErr := function.CaptchaGenerate()
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))

	if capErr != nil {
		commonJson.SetError(capErr.Error())
	}

	// 返回结果数据
	res = &backend.CaptchaDoRes{
		B64s:  b64s,
		IdKey: idKey,
	}
	commonJson.SetSuccess("ok", res)
	commonJson.WriteJson()
	return
}

func (c *commonCtl) RefreshToken(ctx context.Context, req *backend.RefreshTokenDoReq) (res *backend.RefreshTokenDoRes, err error) {
	res = &backend.RefreshTokenDoRes{}
	res.Token, res.Expire = service.Auth().RefreshHandler(ctx)
	commonJson := response.CommonResp(g.RequestFromCtx(ctx))
	// 返回结果数据

	commonJson.SetSuccess("ok", res)
	commonJson.WriteJson()
	return
}
